<?php
// +----------------------------------------------------------------------
// | Qbt CMS Plugin
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.qbt8.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: ranf
// +----------------------------------------------------------------------
namespace plugins\qbt_page_content_filter\controller;
use cmf\controller\PluginAdminBaseController;
use plugins\qbt_page_content_filter\model\ViewModel;
use think\Db;
use think\Request;

class AdminIndexController extends PluginAdminBaseController{

    public function _initialize()
    {
        $this->data = $this->request->param();
        unset($this->data['_plugin']);
        unset($this->data['_controller']);
        unset($this->data['_action']);
        $this->model = new ViewModel();
        $adminId = cmf_get_current_admin_id();//获取后台管理员id，可判断是否登录
        if (!empty($adminId)) {
            $this->assign("admin_id", $adminId);
        } else {
            $this->error('未登录');
        }
    }

    //后台列表页
    public function index(){
        $list = Db::name('view_filter')->select()->toArray();
        $this->assign('list',$list);
        return $this->fetch('/index');
    }

    //添加页
    public function add(){

        return $this->fetch('/add');
    }

    //添加程序
    public function add_action(){
        $this->data['addtime'] = time();
        $this->valie($this->data);
        $status = $this->model->add_data($this->data);
        $this->data_action($status,'add',cmf_plugin_url('QbtPageContentFilter://AdminIndex/index'));
    }

    //修改页
    public function edit(){

        $info = $this->model->data_info('id ='.$this->data['id']);
        $this->assign('info',$info);
        return $this->fetch('/edit');
    }

    //修改程序
    public function edit_action(){
        $this->data['addtime'] = time();
        $this->valie($this->data);
        $status = $this->model->update_data('id ='.$this->data['id'],$this->data);
        $this->data_action($status,'update',cmf_plugin_url('QbtPageContentFilter://AdminIndex/index'));
    }

    //修改状态
    public function setstatus(){
        $status = $this->model->update_data('id ='.$this->data['id'],$this->data);
        $this->data_action($status,'update',cmf_plugin_url('QbtPageContentFilter://AdminIndex/index'));
    }

    //删除数据
    public function del(){
        $status = $this->model->del_data('id ='.$this->data['id']);
        $this->data_action($status,'del',cmf_plugin_url('QbtPageContentFilter://AdminIndex/index'));
    }






    //公共部分
    public function data_action($status,$str,$url=''){
        if($status !== false){
            if($str == 'add'){
                $this->success('保存成功！',$url);
            }else if( $str == 'update'){
                $this->success('更改成功！',$url);
            }else if($str == 'del'){
                $this->success('删除成功！',$url);
            }
        }else{
            $this->error('操作失败！');
        }
    }

    //验证数据
    public function valie($data){
        if(empty($data['rules'])){
            $this->error('规则不能为空!');
        }
        if(!preg_match('/^\/.*?\/$/',$data['rules'])){
            $this->error('请输入正确的正则表达式');
        }
        if(empty($data['content'])){
            $this->error('替换内容不能为空！');
        }

    }



}